$(document).ready(function () {
        // $.ajax({
        //     type:'get',
        //     url:'/getNewArticle',
        //     dataType:'json',
        //     success:function (data) {
        //         if (data['status'] != 1){
        //             alert(data['message'])
        //         }else {
        //             showNewArticle(data['result'])
        //         }
        //
        //     }
        // });
        $.ajax({
            type:'get',
            url:'/getUserInfo',
            dataType:'json',
            success:function (data) {
                if (data['status'] != 0){
                    $(".am-topbar-right").append("<a class='image'href='/user'><img src='" + data['avatarUrl']+ "' style='max-height: 42px;border-radius: 100%;position: absolute;right: 12px;top: -28px;'></a>")
                }
            }
        })

});
ajaxFirst(1);

countVisitor();

$.ajax({
   type:'post',
    url:'/getWebMsg',
    dataType:'json',
    success:function (data) {

        $("#totalVolume_num").append(data['visitorCount']);
        $("#totalVisitor_num").append(data['IPCount'])
    }
});


function ajaxFirst(currentPage) {
    $.ajax({
        type:'get',
        url:'/getAllArticleDesc',
        dataType:'json',
        data:{
            row:'3',
            pageNum:currentPage
        },
        success:function (data) {
            if (data['status'] == 200){
                putArticleInfo(data['result']);
                scrollTo(0,0);
                //分页
                $("#pagination").paging({
                    rows:data['pageInfo']['pageSize'],//每页显示条数
                    pageNum:data['pageInfo']['pageNum'],//当前所在页码
                    pages:data['pageInfo']['pages'],//总页数
                    total:data['pageInfo']['total'],//总记录数
                    callback:function(currentPage){
                        ajaxFirst(currentPage);
                    }
                });
            }else {
                alert(data['message']);
            }
        },
        error:function() {
            alert("请求文章数据失败")
        }
    })
}


function putArticleInfo(data) {
    var insert = $("#insert");
    insert.empty();
    $.each(data, function (index, object) {

        var post = '<li style="list-style-type: none">\n' +
            '\t\t\t\t\t\t\t\t<div id="articlePost">\n' +
            '\n' +
            '\t\t\t\t\t\t\t\t\t<article class="post">\n' +
            '\t\t\t\t\t\t\t\t\t\t<header id="postHeader">\n' +
            '\t\t\t\t\t\t\t\t\t\t\t<div id="postArticle" class="title"><h2><a href="/findArticle?articleID=' + object['id'] + '">' + object['title'] + '</a></h2><p>' + object['summary'] + '</p></div>\n' +
            '\t\t\t\t\t\t\t\t\t\t</header>\n' +
            '\t\t\t\t\t\t\t\t\t\t<footer>\n' +
            '\t\t\t\t\t\t\t\t\t\t\t<ul style="margin-left: -18px;color: #727472"><div id="postTime" class="author" style="color: #727472"><span class="name">' + object['articleCategories'] + '</span><time class="published">' + object['writeTime'] + '</time></div></ul><ul class="actions" id="postRead"><li style="float: right"><a href="/findArticle?articleID=' + object['id'] + '" class="button big" style="font-weight: 100;">阅读全文</a></li></ul>' +
            '\t\t\t\t\t\t\t\t\t\t</footer>\n' +
            '\t\t\t\t\t\t\t\t\t</article>\n' +
            '\t\t\t\t\t\t\t\t</div>\n' +
            '\t\t\t\t\t\t\t\t</li>';
        insert.append(post);

    });

}
//来访几次
function countVisitor() {
    var caution = false
    function setCookie(name, value, expires, path, domain, secure){
        var curCookie = name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires.toGMTString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "")
        if (!caution || (name + "=" + escape(value)).length<= 4000)
            document.cookie = curCookie
        else
        if (confirm("Cookie exceeds 4KB and will be cut!"))
            document.cookie = curCookie
    }
    function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
            return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex+ prefix.length)
        if (cookieEndIndex == -1)
            cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex +prefix.length, cookieEndIndex))
    }
    function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
            document.cookie = name + "=" +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT"
        }
    }
    function fixDate(date) {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0)
            date.setTime(date.getTime() - skew)
    }
    var now = new Date()
    fixDate(now)
    now.setTime(now.getTime() + 10* 365 * 24 * 60 * 60 * 1000)
    visits = getCookie("counter")
    if (!visits)
        visits = 1
    else
        visits = parseInt(visits) + 1
    setCookie("counter", visits, now)
    $('#person_num').append(visits);
}
function showQQ() {


    if ($("#qqInfo").css("display") == 'none') {//如果show是隐藏的

        $("#qqInfo").css("display", "block");//show的display属性设置为block（显示）

    } else {//如果show是显示的

        $("#qqInfo").css("display", "none");//show的display属性设置为none（隐藏）

    }
}
function showWeChat() {


    if ($("#weChatInfo").css("display") == 'none') {//如果show是隐藏的

        $("#weChatInfo").css("display", "block");//show的display属性设置为block（显示）

    } else {//如果show是显示的

        $("#weChatInfo").css("display", "none");//show的display属性设置为none（隐藏）

    }
}
function showWeibo() {


    if ($("#weiboInfo").css("display") == 'none') {//如果show是隐藏的

        $("#weiboInfo").css("display", "block");//show的display属性设置为block（显示）

    } else {//如果show是显示的

        $("#weiboInfo").css("display", "none");//show的display属性设置为none（隐藏）

    }
}


